package com.hl.ruoyilkshop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hl.ruoyilkshop.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyilkshop.domain.Goods;
import com.hl.ruoyilkshop.service.IGoodsService;
import com.hl.ruoyilkshop.domain.dto.CreateGoodsRequest;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/good")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ISearchService searchService;

    // 搜索相关接口 - 放在最前面，避免被通配符路径匹配
    @GetMapping("/search")
    public AjaxResult searchGoods(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return searchService.searchGoods(keyword, pageNum, pageSize);
    }
    
    /**
     * 同步所有商品到搜索索引
     */
    @PostMapping("/syncAllToIndex")
    public AjaxResult syncAllGoodsToIndex() {
        return searchService.syncAllGoodsToIndex();
    }
    
    /**
     * 同步商品到搜索索引
     */
    @PostMapping("/syncToIndex/{goodsId}")
    public AjaxResult syncGoodsToIndex(@PathVariable Long goodsId) {
        return searchService.addGoodsToIndex(goodsId);
    }

    /**
     * 从搜索索引中删除商品
     */
    @DeleteMapping("/deleteFromIndex/{goodsId}")
    public AjaxResult deleteGoodsFromIndex(@PathVariable Long goodsId) {
        return searchService.deleteGoodsFromIndex(goodsId);
    }
    /**
     * 查询商品管理列表
     */
    //@RequiresPermissions("lkshop:good:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @RequiresPermissions("lkshop:good:export")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
   // @RequiresPermissions("lkshop:good:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(goodsService.selectGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品管理
     */
    @RequiresPermissions("lkshop:good:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        int rows = goodsService.insertGoods(goods);
        if (rows > 0) {
            return success(goods.getGoodsId());
        }
        return error("新增失败");
    }

    /**
     * 聚合创建：商品 + 规格项/规格值/关联 + SKU（同一事务）
     */
    @RequiresPermissions("lkshop:good:add")
    @Log(title = "商品管理-聚合创建", businessType = BusinessType.INSERT)
    @PostMapping("/aggregate")
    public AjaxResult addAggregate(@RequestBody CreateGoodsRequest request)
    {
        Long goodsId = goodsService.createGoodsWithSpecsAndSkus(request);
        return success(goodsId);
    }

    /**
     * 修改商品管理
     */
    @RequiresPermissions("lkshop:good:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品管理
     */
    @RequiresPermissions("lkshop:good:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }
}
